import { createStore } from 'vuex'

export default createStore({
    // state:{
    state() {
        return {
            // //storage默认存储为字符串，转换成原本数据格式
            // isLogin: window.sessionStorage.getItem('isLogin'),
            token: window.sessionStorage.getItem('satoken'),
            // phone: window.sessionStorage.getItem('phone'),
            // localPhone: window.localStorage.getItem('phone'),
            autoLogin: window.localStorage.getItem('autoLogin'),
            localToken: window.localStorage.getItem('satoken')
                // localIsLogin: window.localStorage.getItem('isLogin')
        }
    },
    mutations: {
        // 更改store.state
        // $_setPhone(state, value) {
        //     state.phone = value
        //     sessionStorage.setItem('phone', value)
        // },
        $_setToken(state, value) {
            state.token = value
            sessionStorage.setItem('satoken', value)
        },
        // $_setIsLogin(state, value) {
        //     state.isLogin = value
        //     sessionStorage.setItem('isLogin', value)
        // },
        // $_setLocalPhone(state, value) {
        //     state.localPhone = value
        //     localStorage.setItem('phone', value)
        // },
        $_setLocalToken(state, value) {
            state.localToken = value
            localStorage.setItem('satoken', value)
        },
        // $_setLocalIsLogin(state, value) {
        //     state.localIsLogin = value
        //     localStorage.setItem('isLogin', value)
        // },

        autoLogin: (state, value) => {
            state.autoLogin = value
            if (state.autoLogin) { //自动登录
                window.localStorage.setItem('satoken', state.localToken)
                    // window.localStorage.setItem('phone', state.localPhone)
                    // window.localStorage.setItem('isLogin', state.localIsLogin)
                window.sessionStorage.setItem('satoken', state.localToken)
                    // window.sessionStorage.setItem('phone', state.localPhone)
            } else {
                window.localStorage.removeItem('satoken')
                    // window.localStorage.removeItem('phone')
                    // window.localStorage.removeItem('isLogin')
            }
            window.localStorage.setItem('autoLogin', value);
        },
        logout: (state) => {
            state.localIsLogin = null
            state.autoLogin = false
            window.sessionStorage.clear();
            window.localStorage.clear();
            // window.sessionStorage.removeItem('satoken')
            // window.sessionStorage.removeItem('phone')
            // window.sessionStorage.removeItem('isLogin')
            // window.localStorage.removeItem('satoken')
            // window.localStorage.removeItem('phone')
            // window.localStorage.removeItem('isLogin')
            // window.localStorage.setItem('autoLogin', false)
        }
    },
    actions: {},
    modules: {}
})